/*
 * Copyright 2017-8-30 13:56:26 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.admin.ao.impl;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import com.alibaba.fastjson.JSONArray;
import com.lap.scd.commons.ao.AbstractAO;
import com.lap.scd.commons.constant.RespCode;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.admin.ao.ResourcesAO;
import com.lap.scp.admin.bean.TreeDO;
import com.lap.scp.admin.core.ResourcesManager;
import com.lap.scp.admin.domain.ResourcesDO;
import com.lap.scp.admin.query.ResourcesQueryDO;
import com.lap.scp.admin.support.ResourcesSupport;

/**
 * <pre>
 * 资源管理AO
 * </pre>
 * 
 * @author 郭凯
 * @Date 2017-8-30 13:56:26
 * @since 1.0
 */
@Service("resourcesAO")
public class DefaultResourcesAO extends AbstractAO implements ResourcesAO {

	@Resource
	private ResourcesManager resourcesManager;

	@Resource
	private ResourcesSupport resourcesSupport;

	@Override
	public Resp<Integer> deleteById(int resourcesId) {
		Resp<Integer> resp = new Resp<>();
		try {
			int countList = resourcesManager.countByPid(resourcesId);
			if (countList > 0) {
				resp.putMsg(getMsg("RS00001"));
			} else {
				int flag = resourcesManager.deleteById(resourcesId);
				resp.setData(flag);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(resourcesId, e);
		}
		return resp;
	}

	@Override
	public Resp<ResourcesDO> insertResources(ResourcesDO resourcesDO, BindingResult error) {
		Resp<ResourcesDO> resp = new Resp<>();
		try {
			super.validate(resp, error, resourcesDO);
			if (resp.isSuccess()) {
				resourcesManager.insertResources(resourcesDO);
				resp.setData(resourcesDO);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(resourcesDO, e);
		}
		return resp;
	}

	@Override
	public Resp<ResourcesDO> forEdit(int resourcesId) {
		Resp<ResourcesDO> resp = new Resp<>();
		try {
			ResourcesDO resourcesDO = resourcesManager.queryById(resourcesId);
			List<ResourcesDO> queryList = resourcesManager.queryList(null);
			List<TreeDO> treeList = resourcesSupport.getTree(queryList);
			resourcesDO.setTreeList(treeList);
			resp.setData(resourcesDO);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(resourcesId, e);
		}
		return resp;
	}

	@Override
	public String queryList(ResourcesQueryDO queryDO) {
		String json = "{}";
		try {
			List<ResourcesDO> queryList = resourcesManager.queryList(queryDO);
			json = JSONArray.toJSONString(queryList);
		} catch (Exception e) {
			writeLog(queryDO, e);
		}
		return json;
	}

	@Override
	public List<ResourcesDO> queryListAjax(ResourcesQueryDO resourcesQueryDO) {
		List<ResourcesDO> queryList = null;
		try {
			queryList = resourcesManager.queryList(resourcesQueryDO);
		} catch (Exception e) {
			writeLog(resourcesQueryDO, e);
		}
		return queryList;
	}

	@Override
	public Resp<Integer> updateResources(ResourcesDO resourcesDO, BindingResult error) {
		Resp<Integer> resp = new Resp<>();
		try {
			super.validate(resp, error, resourcesDO);
			if (resp.isSuccess()) {
				int flag = resourcesManager.updateResources(resourcesDO);
				resp.setData(flag);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(resourcesDO, e);
		}
		return resp;
	}

	@Override
	public List<TreeDO> forAdd() {
		List<TreeDO> treeResources = Collections.emptyList();
		try {
			List<ResourcesDO> queryList = resourcesManager.queryList(null);
			treeResources = resourcesSupport.getTree(queryList);
		} catch (Exception e) {
			writeLog(e);
		}
		return treeResources;
	}

	@Override
	public List<String> queryUrlList() {
		List<String> list = Collections.emptyList();
		try {
			list = resourcesManager.queryUrlList();
		} catch (Exception e) {
			writeLog(e);
		}
		return list;
	}

	@Override
	public List<String> queryUrlByUserId(int userId) {
		List<String> list = Collections.emptyList();
		try {
			list = resourcesManager.queryByUserId(userId);
		} catch (Exception e) {
			writeLog(e);
		}
		return list;
	}

}
